package com.lkd.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/2/27 18:24
 * @Version 1.0
 */
public class ImageUtil {


    /**
     * 将Base64图片保存到指定文件
     *
     * @param content
     * @param distinctFile
     * @throws Exception
     */
    public static void generateImage(String content,String distinctFile) throws Exception {
        //写死放D盘
        String savePath = "D://";
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] b = decoder.decode(content);
        // 处理数据
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        // 保存图片
        OutputStream out = new FileOutputStream(savePath + distinctFile);
        out.write(b);
        out.flush();
        out.close();
    }

    public static void main(String[] args) throws Exception{

        String json = "{\n" +
                "    \"photoNumForLog\":1,\n" +
                "    \"picturelist\":[\n" +
                "        {\n" +
                "            \"pdfindex\":0,\n" +
                "            \"page\":[\n" +
                "                1\n" +
                "            ],\n" +
                "            \"location\":[\n" +
                "                [\n" +
                "                    0.2668067216873169,\n" +
                "                    0.3377642035484314,\n" +
                "                    0.20438000559806824,\n" +
                "                    0.09917400032281876\n" +
                "                ]\n" +
                "            ],\n" +
                "            \"signature\":[\n" +
                "\"iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyFpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1ND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wjQmsBYMg0ZwFyxKUwLrKX4+P9eBUbA4DQk0gJtgru/7WiZRneQENoEbRcRPxXIwBmqSmEATxc/5R7tl4B5YkqQEmsF1MDtg+6WgLykJbAP9AvEmPoJ9SUigBVwTin8P6sEr2wnsMI+BKWkEfd5S/BvbL/FO0AtmCvq8pvh3tpfR3aBHKP4lWAc+2N7I9oKrwrmf88p/sl1KHABXgCvo84ziP9su5lrBZaH4pxT/RVNOV0co/ijICsU/ARvAV40fWAFegCMRiD8OLgr7PAYbwTeNoVlZULZeAsdCiD8Jzgv7PKL47xpHtgqM+NzPBXBCIf4MOCfs85Cl9A+NpVxDA1HMunWA0wIh0vYmHrCU/qnxxPUUvyCCK6q5Y/cp/pfG1Jvn7TaYF/KZdpXvjHnfNoPf2lOJQyXcT9BVxeUyKV21zPu2BYyHOVZp4UDadd3QCQ4KxximiVGLd1jGjnOgQSA5pmtl/1lgj3DeW2AryIXdZKbq8D8Flq5JWNtIY4h3PbR4/zKa41UZirEeGohSfLGNLMcJBmIQbzzv9ijFT1dK5DhRf4Tz9NE+TkR9VaYr5iYK/GrY6KV9zMfxTJYqp/MFvlUb3WBXXOKDGJo8BXQrxu7i8joZp0sK4sgmKaRLMK7Z2PaD2H+EDmopPQrqDNA2y125LL+gSzyxR2HZEm1MjXS4XOI1pt6jwGIW0VSpbeU+o9GeSrT5ymrjE9otnDGJziT90c79wmx8Zx1LURWy/ynHcrhp/7vNXwEGABPxi7aDGtrgAAAAAElFTkSuQmCC\"           \n" +
                "]\n" +
                "        }\n" +
                "    ],\n" +
                "    \"datajson\":{\n" +
                "        \"email\":\"\",\n" +
                "        \"orderid\":\"631080152\",\n" +
                "        \"isQuery\":\"false\"\n" +
                "    }\n" +
                "}";


        JSONObject param = JSON.parseObject(json);
        JSONArray picArray = param.getJSONArray("picturelist");
        for(int i=0;i<picArray.size();i++){
            JSONObject picObject = picArray.getJSONObject(i);

            List<String> signatureList = picObject.getObject("signature", List.class);
            for(String str : signatureList){
                //文件规则暂时随机生成
                generateImage(str,UUID.randomUUID() + ".jpg");
            }
        }
    }

}